package com.binfast.codegen.gen;

import cn.hutool.core.util.StrUtil;
import org.beetl.core.Template;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.db.TableDesc;
import org.beetl.sql.ext.gen.GenConfig;
import org.beetl.sql.ext.gen.MapperCodeGen;
import org.beetl.sql.ext.gen.SourceGen;

import java.io.IOException;

public class CommonCodeGen extends MapperCodeGen {
    String pkg = null;
    private String mapperTemplate = "";
    private String classNameSuffix = "";//类名,同时也是文件名的后缀,例如AdminDao->Dao就是后缀
    private String classTitle = "";//类标题,例如:AdminUser->用户
    private Boolean isCreateFolder = false;//是否创建子目录文件夹
    private String servicePkg = "";
    private String dtoParamPkg = "";

    private static String srcPathRelative = "/main/java";
    private static String resourcePathRelative = "/main/resources";
    /**
     * 构造函数
     * @param pkg 包名
     * @param template 模板路径
     * @param suffix 类名/文件名后缀
     */
    public CommonCodeGen(String pkg,String template, String suffix) {
        this.pkg = pkg;
        this.mapperTemplate = (new GenConfig()).getTemplate(template);
        this.classNameSuffix = suffix;
    }
    /**
     * 构造函数
     * @param pkg 包名
     * @param template 模板路径
     * @param suffix 类名/文件名后缀
     */
    public CommonCodeGen(String pkg,String template, String suffix,String servicePkg,String dtoParamPkg) {
        this.pkg = pkg;
        this.mapperTemplate = (new GenConfig()).getTemplate(template);
        this.classNameSuffix = suffix;
        this.servicePkg = servicePkg;
        this.dtoParamPkg = dtoParamPkg;
    }
    /**
     * 构造函数
     * @param pkg 包名
     * @param template 模板路径
     * @param suffix 类名/文件名后缀
     */
    public CommonCodeGen(String pkg,String template, String suffix,String classTitle,String servicePkg,String dtoParamPkg) {
        this.pkg = pkg;
        this.mapperTemplate = (new GenConfig()).getTemplate(template);
        this.classNameSuffix = suffix;
        this.classTitle = classTitle;
        this.servicePkg = servicePkg;
        this.dtoParamPkg = dtoParamPkg;
    }

    @Override
    public String getMapperTemplate() {
        return mapperTemplate;
    }

    @Override
    public void setMapperTemplate(String mapperTemplate) {
        this.mapperTemplate = mapperTemplate;
    }

    public void setClassNameSuffix(String suffix){
        this.classNameSuffix = suffix;
    }

    public void genCode(SQLManager sqlManager,String preTableName, String tableName,Boolean isCreateFolder){
        this.isCreateFolder = isCreateFolder;
        String entityClass = StrUtil.upperFirst(StrUtil.toCamelCase(tableName));
        genCode(this.pkg,entityClass,sqlManager.getMetaDataManager().getTable(preTableName+tableName),null,false);
    }
    public void genCode(SQLManager sqlManager,String preTableName,String tableName){
        String entityClass = StrUtil.upperFirst(StrUtil.toCamelCase(tableName));
        genCode(this.pkg,entityClass,sqlManager.getMetaDataManager().getTable(preTableName+tableName),null,false);
    }
    @Override
    public void genCode(String entityPkg, String entityClass, TableDesc tableDesc, GenConfig config, boolean isDisplay) {
        if (this.pkg == null) {
            this.pkg = entityPkg;
        }
        String camelClassName = StrUtil.lowerFirst(entityClass);
        if(this.isCreateFolder) {
            this.pkg += "." + camelClassName;
        }
        String comment = tableDesc.getRemark();
        if(StrUtil.isNotBlank(this.classTitle)) comment = this.classTitle;
        Template template = SourceGen.getGt().getTemplate(this.mapperTemplate);
        template.binding("tableName", tableDesc.getName());
        template.binding("className", entityClass);
        template.binding("camelClassName", camelClassName);
        template.binding("package", this.pkg);
        template.binding("servicePkg", this.servicePkg);
        template.binding("dtoParamPkg", this.dtoParamPkg);
        template.binding("comment", comment);
        String mapperHead = "import " + entityPkg + ".*;" + SourceGen.CR;
        template.binding("imports", mapperHead);
        String mapperCode = template.render();
        if (isDisplay) {
            System.out.println(mapperCode);
        } else {
            try {
              String path = getJavaSRCPath();
                SourceGen.saveSourceFile(path, this.pkg, entityClass+this.classNameSuffix, mapperCode);
            } catch (IOException var11) {
                throw new RuntimeException("mapper代码生成失败", var11);
            }
        }

    }

    public static void setSrcPathRelativeToSrc(String srcPathRelativeToSrc) {
        srcPathRelative = srcPathRelativeToSrc;
    }

    public static void setResourcePathRelativeToSrc(String resourcePathRelativeToSrc) {
        resourcePathRelative = resourcePathRelativeToSrc;
    }

    public static String getJavaSRCPath() {
        return getPath(srcPathRelative);
    }

    public static String getJavaResourcePath() {
        return getPath(resourcePathRelative);
    }

    public static String getPath(String relativeToSrc) {
        String userDir = System.getProperty("user.dir");
        if (userDir == null) {
            throw new NullPointerException("用户目录未找到");
        } else {
            return userDir + relativeToSrc;
        }
    }
}
